//
//  LHCUpdateViewController.m
//  MyAddressBook
//
//  Created by lihongcheng on 16/4/22.
//  Copyright © 2016年 lihongcheng. All rights reserved.
//

#import "LHCUpdateViewController.h"

@interface LHCUpdateViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITextField *nameField;
@property (nonatomic,strong) UITextField *phoneField;
@property (nonatomic,strong) UITextField *ageField;
@property (nonatomic,strong) UITextField *emailField;
@property (nonatomic,strong) UITextField *addressField;

@end

@implementation LHCUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createMainView];
    
}

-(void)createMainView
{
    self.navigationItem.title = @"修改信息";
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.backView];
    
    NSArray *array = @[@"姓名:",@"手机:",@"年龄:",@"邮箱:",@"地址:"];
    for (int i = 0; i < array.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50+50*i, 50, 20)];
        label.text = array[i];
        [self.backView addSubview:label];
    }
    
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(110, 40, ScreenWidth-140, 40)];
    self.nameField.placeholder = @"请输入姓名";
    self.nameField.borderStyle = UITextBorderStyleRoundedRect;
    self.nameField.delegate = self;
    self.nameField.text = self.model.name;
    [self.backView addSubview:self.nameField];
    
    self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(110, 40+50, ScreenWidth-140, 40)];
    self.phoneField.placeholder = @"请输入手机号";
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneField.borderStyle = UITextBorderStyleRoundedRect;
    self.phoneField.delegate = self;
    self.phoneField.text = self.model.phone;
    [self.backView addSubview:self.phoneField];
    
    self.ageField = [[UITextField alloc] initWithFrame:CGRectMake(110, 40+50*2, ScreenWidth-140, 40)];
    self.ageField.placeholder = @"请输入年龄";
    self.ageField.borderStyle = UITextBorderStyleRoundedRect;
    self.ageField.delegate = self;
    self.ageField.text = self.model.age;
    [self.backView addSubview:self.ageField];
    
    self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(110, 40+50*3, ScreenWidth-140, 40)];
    self.emailField.placeholder = @"请输入邮箱";
    self.emailField.borderStyle = UITextBorderStyleRoundedRect;
    self.emailField.delegate = self;
    self.emailField.text = self.model.email;
    [self.backView addSubview:self.emailField];
    
    self.addressField = [[UITextField alloc] initWithFrame:CGRectMake(110, 40+50*4, ScreenWidth-140, 40)];
    self.addressField.placeholder = @"请输入地址";
    self.addressField.borderStyle = UITextBorderStyleRoundedRect;
    self.addressField.delegate = self;
    self.addressField.text = self.model.address;
    [self.backView addSubview:self.addressField];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sureBtn.frame = CGRectMake(100, 350, ScreenWidth-200, 40);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.backgroundColor = [UIColor orangeColor];
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:sureBtn];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.backView addGestureRecognizer:tap];
    
    
    
}

-(void)sureBtnClick:(UIButton *)btn
{
    if (self.nameField.text.length == 0) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"姓名不能为空" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alter show];
        return;
    }
    if (self.phoneField.text.length == 0) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"手机号不能为空" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alter show];
        return;
    }
    LHCDataModel *model = [[LHCDataModel alloc] init];
    model.name = self.nameField.text;
    model.phone = self.phoneField.text;
    model.age = self.ageField.text;
    model.email = self.emailField.text;
    model.address = self.addressField.text;
    BOOL b = [LHCSqlOperate updateDataFromSql:model andbefore:self.model];
    if (b == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"修改失败" message:@"请再试一次" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alter show];
    }
    
}

-(void)tapClick:(UITapGestureRecognizer *)tap
{
    [self.nameField resignFirstResponder];
    [self.phoneField resignFirstResponder];
    [self.ageField resignFirstResponder];
    [self.emailField resignFirstResponder];
    [self.addressField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
